package com.cqc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter;
import org.springframework.web.filter.ServletContextRequestLoggingFilter;

/**
 * @author cui
 * @date 2024/11/13 21:21
 * @description
 **/
@Configuration
public class RequestLoggingFilter {
    /**
     * <a href="https://mp.weixin.qq.com/s/tjAa2N38OnxN4BcfwlO79w">请不要自己写，Spring Boot非常实用的内置功能</a>
     * requestLoggingFilter() 会报错
     * 开发者可以自定义记录请求的参数、请求体、请求头和客户端信息。
     * 需要配置 logging.level.org.springframework.web.filter.CommonsRequestLoggingFilter=debug
     */
    @Bean
    public CommonsRequestLoggingFilter loggingFilter() {
        CommonsRequestLoggingFilter loggingFilter = new CommonsRequestLoggingFilter();
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setIncludeHeaders(true);
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setBeforeMessagePrefix("HTTP Request: ");
        return loggingFilter;
    }

    /**
     * 无需配置 logging.level.org.springframework.web.filter.ServletContextRequestLoggingFilter=debug
     */
    @Bean
    public ServletContextRequestLoggingFilter loggingFilter2() {
        ServletContextRequestLoggingFilter loggingFilter = new ServletContextRequestLoggingFilter();
        loggingFilter.setIncludeQueryString(true);
        loggingFilter.setIncludePayload(true);
        loggingFilter.setIncludeHeaders(true);
        loggingFilter.setIncludeClientInfo(true);
        loggingFilter.setBeforeMessagePrefix("HTTP Request: ");
        return loggingFilter;
    }
}
